/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.shinyray.project.bpm;

import com.shinyray.project.bpm.activity.ApplyActivity;
import com.shinyray.project.bpm.activity.ApproveActivity;
import com.shinyray.project.bpm.activity.ArchiveActivity;

import org.jbpm.env.Environment;
import org.jbpm.env.EnvironmentFactory;
import org.jbpm.env.session.PvmDbSession;
import org.jbpm.pvm.ProcessDefinition;
import org.jbpm.pvm.ProcessFactory;
import org.jbpm.stream.ResourceStreamSource;


/**
 * 创建项目流程的工具类。
 * @author Administrator
 */
public class ProjectBPMHelper {
   private static ProcessDefinition flogisticProjectProcessDef=null;
   private static EnvironmentFactory environmentFactory = EnvironmentFactory.parse(new ResourceStreamSource(
        "environment.cfg.xml"
    ));
    /**
     * 获取项目审批的流程
     * @return
     */
    public static ProcessDefinition getProjectApproveProcessDefinition()
    {
        if(flogisticProjectProcessDef!=null)
        {
            return flogisticProjectProcessDef;
        }
        Environment environment = environmentFactory.openEnvironment();
        try{
            PvmDbSession pvmDbSession = environment.get(PvmDbSession.class);
            ProcessDefinition pdef=pvmDbSession.findProcessDefinition("融资物流项目审批");
            if(pdef==null)
            {
                pdef=ProcessFactory.build("融资物流项目审批")
                        .node("项目申请").initial().behaviour(new ApplyActivity())
                            .transition().to("项目经理审批")
                        .node("项目经理审批").behaviour(new ApproveActivity())
                            .transition().to("部门副经理审批")
                        .node("部门副经理审批").behaviour(new ApproveActivity())
                            .transition().to("总经理审批")
                        .node("总经理审批").behaviour(new ApproveActivity())
                            .transition().to("项目归档")
                        .node("项目归档").behaviour(new ArchiveActivity())
                .done();
                pvmDbSession.save(pdef);
            }
            flogisticProjectProcessDef=pdef;
        }finally{
            environment.close();
        }
        return flogisticProjectProcessDef;
    }

}
